<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>business</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/font-awesome.min.css" media="all">


    <style>
        .layui-form-switch {
            width: 54px;
            margin-top: 0;
        }
        .layui-form-switch i{
            top: 2px;
        }
    </style>
</head>

<body>
<div style="padding: 20px; position: relative;">
    <!--<div class="layui-btn-group operateTable">
        <button class="layui-btn" data-type="getCheckData">获取选中行数据</button>
        <button class="layui-btn" data-type="getCheckLength">获取选中数目</button>
        <button class="layui-btn" data-type="isAll">验证是否全选</button>
    </div>-->
    <div class="operateTable">
        名称：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="filter-id" placeholder="请输入关键字" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
    <div class="operateTable" style="position: absolute; top: 20px; right: 20px;">
        <button class="layui-btn" data-type="excel">导出Excel</button>
        <button class="layui-btn  AUTH-SQGL_SQXZ" data-type="addBusiness"><i class="layui-icon"></i> 新增商圈</button>
    </div>
    <table class="layui-table" lay-data="{height:'full-80', url:'/business/json/', page:true, limit:20, id:'businessTable'}"
           lay-filter="business">
        <thead>
        <tr>
            <!--<th lay-data="{checkbox:true, fixed: true}"></th>-->
            <th lay-data="{field:'ROWNUM', fixed: true}"></th>
            <th lay-data="{field:'BUSINESS_NAME', width:150}">名称</th>
            <!--<th lay-data="{field:'BUSINESS_AREA', width:100}">面积(万m²)</th>-->
            <th lay-data="{field:'BUSINESS_ADDRESS', width:250}">位置</th>
            <th lay-data="{field:'BUSINESS_LNG', width:120}">经度</th>
            <th lay-data="{field:'BUSINESS_LAT', width:120}">纬度</th>
            <th lay-data="{field:'BUSINESS_INFO', width:150}">备注</th>
            <th lay-data="{field:'CREATE_NAME', width:100}">创建人</th>
            <th lay-data="{field:'CREATE_DATE', width:180, align:'center'}">创建时间</th>
            <th lay-data="{field:'IS_OPEN', templet:'#statusTpl', width:100, align:'center'}">是否开启</th>
            <th lay-data="{fixed: 'right', width:200, align:'center', toolbar: '#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>
    <!--<script type="text/html" id="barDemo">-->
    <div id="barDemo" style="display: none">
        <a class="layui-btn layui-btn-primary layui-btn-mini AUTH-SQGL_SQCK" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-mini AUTH-SQGL_SQBJ" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-mini AUTH-SQGL_SQSC" lay-event="del">删除</a>
    </div>
   <!-- </script>-->
</div>

<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    layui.use(['jquery', 'table', 'form', 'layer'], function () {
        var table = layui.table,
            form = layui.form;
        //监听工具条
        table.on('tool(business)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.open({
                    type: 2,
                    title: data.BUSINESS_NAME,
                    shadeClose: false,
                    shade: [0.3],
                    maxmin: true, //开启最大化最小化按钮
                    area: ['900px', '90%'],
                    scrollbar: false, //屏蔽滚动条
                    content: 'operate.html?type=view&id=' + data.ID
                });
            } else if (obj.event === 'del') {
                layer.confirm('确认删除 '+data.BUSINESS_NAME+' 吗', {offset: '150px'}, function (index) {
//                    obj.del();
                    layer.close(index);
                    $.ajax({
                        url: "/business/editOpenDelete",
                        data: {ID: data.ID, IS_DELETED: 1},
                        dataType: "json",
                        type: "post",
                        async: false,
                        success: function (res) {
                            if (res.errcode === "0"){
                                active.reload();    // 表格刷新
                                layer.msg(data.BUSINESS_NAME + " 已删除", {offset: '150px'});   // 弹出删除成功
                            } else if (res.errcode === "1"){
                                layer.msg(res.errmsg, function(){});
                            }
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            console.error(XMLHttpRequest.status);
                            console.error(XMLHttpRequest.readyState);
                            console.error(textStatus);
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: "编辑 " + data.BUSINESS_NAME,
                    shadeClose: false,
                    shade: [0.3],
                    maxmin: true, //开启最大化最小化按钮
                    area: ['900px', '90%'],
                    scrollbar: false, //屏蔽滚动条
                    content: 'operate.html?type=edit&id=' + data.ID
                });
            }
        });

        var $ = layui.$, active = {
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('businessTable')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            , getCheckLength: function () { //获取选中数目
                var checkStatus = table.checkStatus('businessTable')
                    , data = checkStatus.data;
                layer.msg('选中了：' + data.length + ' 个');
            }
            , isAll: function () { //验证是否全选
                var checkStatus = table.checkStatus('businessTable');
                layer.msg(checkStatus.isAll ? '全选' : '未全选')
            }
            , reload: function () {
                var name = $('#filter-id').val();
                table.reload('businessTable', {
                    where: {
                        BUSINESS_NAME: name
                    }
                });
            }
            , addBusiness: function () {
                layer.open({
                    type: 2,
                    title: '新增商圈',
                    shadeClose: false,
                    shade: [0.3],
                    maxmin: true, //开启最大化最小化按钮
                    area: ['900px', '90%'],
                    scrollbar: false, //屏蔽滚动条
                    content: 'operate.html?type=add'
                });
            }
            , excel: function () {
                window.location.href = "/business/excel";
            }
        };

        $('.operateTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听指定开关
        form.on('switch(switchTest)', function(data){
            var ID = data.othis.parent().find("input[name='IS_OPEN']").val();
            var name = data.othis.parent().find("input[name='IS_OPEN']").data("name");
            var open = "on";
            if (this.checked === true){
                open = "on";
            } else if (this.checked === false){
                open = "off";
            }
            $.ajax({
                url: "/business/editOpenDelete",
                data: {ID: ID, IS_OPEN: open},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0"){
                        active.reload();    // 表格刷新
                        if (open === "on"){
                            layer.msg(name + ' 已启用', {offset: '150px'});
                        } else if (open === "off"){
                            layer.msg(name + ' 已关闭', {offset: '150px'});
                        }
                    } else if (res.errcode === "1"){
                        layer.msg(res.errmsg, function(){});
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        });
    });
</script>
<script type="text/html" id="statusTpl">
    {{# var status=d.IS_OPEN }}
    {{# var id=d.ID }}
    {{# var name=d.BUSINESS_NAME }}
    {{# if (status=='on'){ }}
    <input type="checkbox" checked="" value="{{id}}" data-name="{{name}}" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">
    {{# } else{ }}
    <input type="checkbox" value="{{id}}" data-name="{{name}}" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">
    {{# } }}
</script>
<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../../static/js/common/authUtils.js"></script>
</body>

</html>